﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtility;

namespace DataAccess
{
    public class CommonExecute
    {
        /// <summary>
        /// 获取表对应的序列号
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public static int GetID(string tableName)
        {
            string sql = string.Format("select {0}_SE.Nextval from dual", tableName);
            object obj = DbHelperOra.GetSingle(sql);
            if (obj != null)
                return Convert.ToInt32(obj);
            else
                return 0;
        }
        /// <summary>
        /// 获取对应介质的介质编号
        /// </summary>
        /// <param name="type">介质类型（1为硬盘，2为cd，3为dvd，4为胶片，5为其他）</param>
        /// <param name="isSky">空标志：0为空，1为非空</param>
        /// <returns>硬盘条码：年代+介质类型+空标识+流水号</returns>
        public static string GetBarCode(string type, string isSky)
        {
            string insertSql = "insert into TB_BARCODE(YEAR,TYPE,ISSKY,SERIALNUMBER) values ({0},{1},{2},{3})";
            int serialnumber = 1;
            string result = "";
            string sql = string.Format("select max(serialnumber) from tb_barcode where year={0} and type={1}", DateTime.Now.Year, type);
            object obj = DbHelperOra.GetSingle(sql);
            if (obj != null)
            {
                serialnumber = (Convert.ToInt32(obj) + 1);
                result = DateTime.Now.Year + type + isSky + serialnumber.ToString().PadLeft(5, '0');
            }
            else
            {
                result = DateTime.Now.Year + type + isSky + serialnumber.ToString().PadLeft(5, '0');
            }
            insertSql = string.Format(insertSql, DateTime.Now.Year, type, isSky, serialnumber);
            DbHelperOra.Query(insertSql);
            return result;
        }

        /// <summary>
        /// 获取流水号:年份+5位流水号
        /// </summary>
        /// <returns></returns>
        public static decimal GetLSH()
        {
            string insertSql = "insert into TB_BARCODE(YEAR,TYPE,ISSKY,SERIALNUMBER) values ({0},9,0,{1})";
            int serialnumber = 1;
            string result = "0";
            string sql = string.Format("select max(serialnumber) from tb_barcode where year={0} and type=9", DateTime.Now.Year);
            object obj = DbHelperOra.GetSingle(sql);
            if (obj != null)
            {
                serialnumber = (Convert.ToInt32(obj) + 1);
                result = DateTime.Now.Year + serialnumber.ToString().PadLeft(5, '0');
            }
            else
            {
                result = DateTime.Now.Year + serialnumber.ToString().PadLeft(5, '0');
            }
            insertSql = string.Format(insertSql, DateTime.Now.Year, serialnumber);
            DbHelperOra.Query(insertSql);
            return Convert.ToDecimal(result);
        }
    }
}
